package com.tianqu.tms.core.webservice.weichat.api.impl;


import java.rmi.RemoteException;

import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.enums.ApiLogFuncType;
import com.tianqu.tms.core.util.enums.WeChatResultCode;
import com.tianqu.tms.core.webservice.weichat.api.CarManager;

/**
 * @author Huang Wei
 *
 */
public class CarManagerImpl extends AbsoluteManager implements CarManager{

	private static CarManagerImpl INSTANCE = new CarManagerImpl();
	
	private CarManagerImpl(){};
	
	@Override
	public void addCar(String licensePlate,String serial,String vendorId,String transferSerial,int mileage,String brandId,String carStyleId,String memberId,int color,int price,String errorMessage) {
		int r = WeChatResultCode.SUCCESS.getValue();
		try {
			StringBuffer log = new StringBuffer();
			log.append("CarManagerImpl.addCar execute,");
			log.append(" licensePlate :"+licensePlate);
			log.append(" serial :"+serial);
			log.append(" vendorId :"+vendorId);
			log.append(" transferSerial :"+transferSerial);
			log.append(" mileage :"+mileage);
			log.append(" brandId :"+brandId);
			log.append(" carStyleId :"+carStyleId);
			log.append(" memberId :"+memberId);
			log.append(" brandId :"+brandId);
			log.append(" color :"+color);
			log.append(" price :"+price);
			LOGGER.info(log);
			//
			r = getCarService().addCar(StringUtil.filterNull(serial), 
					StringUtil.filterNull(transferSerial), 
					StringUtil.filterNull(licensePlate), 
					mileage, 
					StringUtil.filterNull(vendorId), 
					StringUtil.filterNull(brandId), 
					StringUtil.filterNull(carStyleId), 
					color, 
					price, 
					StringUtil.filterNull(memberId));
			if(WeChatResultCode.SUCCESS != WeChatResultCode.getResultCode(r)){
				LOGGER.error(log+" result:"+r);
				sendMobileMessage(errorMessage+",失败原因为"+WeChatResultCode.getResultCode(r).getMessage()+".");
			} 
		}catch (RemoteException e) {
			LOGGER.error("send message error.",e);
			r = WeChatResultCode.ERROR_1004.getValue();
			sendMobileMessage(errorMessage+",失败原因为服务请求超时.");
		}catch (Exception e) {
			LOGGER.error("send message error.",e);
			r = WeChatResultCode.ERROR_1002.getValue();
			sendMobileMessage(errorMessage+",失败原因为系统异常.");
		}
		
		//api log
		StringBuffer apiLog = new StringBuffer();
		apiLog.append("licensePlate_"+licensePlate).append("_");
		apiLog.append("serial_"+serial).append("_");
		apiLog.append("vendorId_"+vendorId).append("_");
		apiLog.append("transferSerial_"+transferSerial).append("_");
		apiLog.append("mileage_"+mileage).append("_");
		apiLog.append("brandId_"+brandId).append("_");
		apiLog.append("carStyleId_"+carStyleId).append("_");
		apiLog.append("memberId_"+memberId).append("_");
		apiLog.append("brandId_"+brandId).append("_");
		apiLog.append("color_"+color).append("_");
		apiLog.append("price_"+price);
		if(r!=WeChatResultCode.SUCCESS.getValue()) saveApiLog(ApiLogFuncType.INVENTORY,apiLog.toString(),WeChatResultCode.getResultCode(r).getMessage());
	}

	public static CarManager create() {
		return INSTANCE;
	}
	
 	@Override
	public void lockOrUnLockCar(String serial, int registerStatus,String errorMessage) {
		int r = WeChatResultCode.SUCCESS.getValue();
		try {
			StringBuffer log = new StringBuffer();
			log.append("CarManagerImpl.lockOrUnLockCar execute,");
			log.append(" registerStatus :"+registerStatus);
			log.append(" serial :"+serial);
			LOGGER.info(log);
			//
			r = getCarService().lockOrUnLockCar(StringUtil.filterNull(serial),registerStatus);
			if(WeChatResultCode.SUCCESS != WeChatResultCode.getResultCode(r)){
				LOGGER.error(log+" result:"+r);
				sendMobileMessage("TMS调用微信“挂起/解挂”接口失败,车辆编号号为"+serial+",失败原因为"+WeChatResultCode.getResultCode(r).getMessage()+".");
			}
		}catch (RemoteException e) {
			LOGGER.error("send message error.",e);
			r = WeChatResultCode.ERROR_1004.getValue();
			sendMobileMessage("TMS调用微信“挂起/解挂”接口失败,车辆编号号为"+serial+",失败原因为服务请求超时.");
		}catch (Exception e) {
			LOGGER.error("send message error.",e);
			r = WeChatResultCode.ERROR_1002.getValue();
			sendMobileMessage("TMS调用微信“挂起/解挂”接口失败,车辆编号号为"+serial+",失败原因为未知异常.");
		}
		//api log
		StringBuffer apiLog = new StringBuffer();
		apiLog.append("registerStatus_"+registerStatus).append("_");
		apiLog.append("serial_"+serial);
		if(r!=WeChatResultCode.SUCCESS.getValue()) saveApiLog(ApiLogFuncType.INVENTORY,apiLog.toString(),WeChatResultCode.getResultCode(r).getMessage());
	}

	@Override
	public void changeRegisterStatus(boolean isBatchCall,boolean isSendMsg, String serial, String memberId, int registerStatus,String errorMessage) {
		int r = WeChatResultCode.SUCCESS.getValue();
		try {
			StringBuffer log = new StringBuffer();
			log.append("CarManagerImpl.changeRegisterStatus execute,");
			log.append(" registerStatus :"+registerStatus);
			log.append(" serial :"+serial);
			log.append(" memberId :"+memberId);
			LOGGER.info(log);
			//
			r = getCarService().changeRegisterStatus(StringUtil.filterNull(serial),registerStatus,StringUtil.filterNull(memberId));
			if(WeChatResultCode.SUCCESS != WeChatResultCode.getResultCode(r)){
				LOGGER.error(log+" result:"+r);
				if(isSendMsg) sendMobileMessage(isBatchCall,"TMS调用微信“入库登记”接口失败,车辆编号号为"+serial+",失败原因为"+WeChatResultCode.getResultCode(r).getMessage()+".");
			} 
		}catch (RemoteException e) {
			LOGGER.error("send message error.",e);
			r = WeChatResultCode.ERROR_1004.getValue();
			if(isSendMsg) sendMobileMessage(isBatchCall,"TMS调用微信“入库登记”接口失败,车辆编号号为"+serial+",失败原因为服务请求超时.");
		}catch (Exception e) {
			LOGGER.error("send message error.",e);
			r = WeChatResultCode.ERROR_1002.getValue();
			if(isSendMsg) sendMobileMessage(isBatchCall,"TMS调用微信“入库登记”接口失败,车辆编号号为"+serial+",失败原因为未知异常.");
		}
		//api log
		StringBuffer apiLog = new StringBuffer();
		apiLog.append("registerStatus_"+registerStatus).append("_");
		apiLog.append("serial_"+serial).append("_");
		apiLog.append("memberId_"+memberId);
		if(r!=WeChatResultCode.SUCCESS.getValue() && !isBatchCall) saveApiLog(ApiLogFuncType.INVENTORY,apiLog.toString(),WeChatResultCode.getResultCode(r).getMessage());
	}
}
